package fruitsManage;

/**
 * 水果
 * 
 * @author 伟❤志
 *
 */
public class FruitsModel {

	private String name;// 水果名
	private double stock;// 库存
	private double price;// 售价
	private double bid;// 进价--不可修改

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getStock() {
		return stock;
	}

	public void setStock(double stock) {
		this.stock = stock;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getBid() {
		return bid;
	}

	public void setBid(double bid) {
		this.bid = bid;
	}

	/**
	 * 查询全部水果信息
	 */
	public void selectAll() {
		for (FruitsModel fruit : Fruits.fruits) {
			fruitInformation(fruit);
		}
	}

	/**
	 * 根据水果名查询水果信息
	 * 
	 * @param name
	 * @return
	 */
	public void selectByName(String name) {
		for (FruitsModel fruit : Fruits.fruits) {
			if (fruit.getName().equals(name)) {
				fruitInformation(fruit);
			}
		}
	}

	/**
	 * 根据水果名查水果id
	 * 
	 * @param name
	 * @return
	 */
	public int searchId(String name) {
		for (int i = 0; i < Fruits.fruits.size(); i++) {
			if (name.equals(Fruits.fruits.get(i).getName())) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 水果信息
	 * 
	 * @param fruitsModel
	 */
	public void fruitInformation(FruitsModel fruitsModel) {
		System.out.println("=================");
		System.out.println("水果名：" + fruitsModel.getName());
		System.out.println("库存：" + fruitsModel.getStock() + " 斤");
		System.out.println("售价：" + fruitsModel.getPrice() + " 元/斤");
		System.out.println("进价：" + fruitsModel.getBid() + " 元/斤");
		System.out.println("=================");
	}

	/**
	 * 添加水果种类
	 * 
	 * @param name
	 * @param stock
	 * @param price
	 * @param bid
	 */
	public void addFruits(String name, double stock, double price, double bid) {

		FruitsModel fruitsModel = new FruitsModel();

		int id = fruitsModel.searchId(name);// 水果id

		// 判断集合中是否已经存在同种水果--名字、售价、进价都要相同
		if (id != -1 && Fruits.fruits.get(id).getBid() == bid && Fruits.fruits.get(id).getPrice() == price) {
			double oStock = Fruits.fruits.get(id).getStock();
			Fruits.fruits.get(id).setStock(stock + oStock);
			return;
		}

		// 添加新品种的水果信息
		fruitsModel.setName(name);
		fruitsModel.setPrice(price);
		fruitsModel.setBid(bid);
		fruitsModel.setStock(stock);

		Fruits.fruits.add(fruitsModel);

		System.out.println();

		fruitsModel.selectByName(name);

	}
	
	/**
	 * 修改水果信息
	 * @param id
	 * @param name
	 * @param stock
	 * @param price
	 */
	public void updateFruit(int id,String name, double stock, double price) {
		
		Fruits.fruits.get(id).setName(name);
		Fruits.fruits.get(id).setStock(stock);
		Fruits.fruits.get(id).setPrice(price);
		
	}

}
